﻿using System;

namespace UnityEngine.Rendering.Universal
{
    [Serializable,VolumeComponentMenu("Post-processing/DirectionBlur")]
    public class DirectionBlur : VolumeComponent, IPostProcessComponent
    {
        public BoolParameter ActiveOn = new BoolParameter(false, true);
        public ClampedFloatParameter blurRadius = new ClampedFloatParameter(1f, 0.0f, 5.0f);
        public ClampedIntParameter interation = new ClampedIntParameter(12, 1, 30);
        public ClampedFloatParameter angle = new ClampedFloatParameter(0.0f, 0.0f, 360f);
        
        static readonly int m_ParamsId = Shader.PropertyToID("_Params");
        
        public bool IsActive()
        {
            return active && ActiveOn.value;
        }

        public bool IsTileCompatible()
        {
            return false;
        }
        
        public override void SetActive(bool value)
        {
            base.SetActive(value);
            ActiveOn.overrideState = true;
            ActiveOn.value = value;
        }

        public override void UpdateVector3(float f1, float f2, float f3, int type = 0)
        {
            blurRadius.overrideState = true;
            blurRadius.value = f1;

            interation.overrideState = true;
            interation.value = (int)f2;

            angle.overrideState = true;
            angle.value = f3;
        }

        public void Render(CommandBuffer cmd, Material material, RTHandle source, RTHandle destination)
        {
            var sinVal = (Mathf.Sin(Mathf.Deg2Rad * angle.value) * blurRadius.value * 0.05f) / interation.value;
            var cosVal = (Mathf.Cos(Mathf.Deg2Rad * angle.value) * blurRadius.value * 0.05f) / interation.value;
            material.SetVector(m_ParamsId, new Vector4(interation.value, sinVal, cosVal, 0.0f));
            cmd.Blit(source, BlitDstDiscardContend(cmd, destination), material);
        }

    }
}